package tests;

import java.sql.SQLException;
import java.util.List;

import persistencia.AgenteBD;

import junit.framework.TestCase;
import dominio.Controller;
import dominio.Usuario;

public class TestUserManagement extends TestCase {

	public void testLogin() {
		try {
			Usuario u = Controller.login("user", "pass");
			assertTrue(u != null);
		} catch (SQLException e) {
			fail("SQLException: " + e.getMessage());
		}
	}

	public void testCreateUser() {
		String name = ("" + Math.random()).substring(0, 10);
		String login = ("" + Math.random()).substring(0, 10);
		String pass = ("" + Math.random()).substring(0, 10);
		try {
			Controller.createNewUser(name, login, pass, false);
			Usuario u = Controller.login(login, pass);
			assertTrue(u != null);
		} catch (SQLException e) {
			fail("SQLException: " + e.getMessage());
		}
	}
	
	public void testGetAllUsers(){
		try {
			List<Usuario> list = Controller.getAllUsers();
			assertTrue(list.size() > 0);
		} catch (SQLException e) {
			fail("SQLException: " + e.getMessage());			
		}
	}
	
	public void testDeleteUser(){		
		try {
			List<Usuario> list = Controller.getAllUsers();
			Controller.deleteUser(list.get(0).getLogin());
			List<Usuario> list2 = Controller.getAllUsers();
			assertTrue(list.size() == list2.size()+1);
		} catch (SQLException e) {
			fail("SQLException: " + e.getMessage());			
		}
	}

}
